import 'dart:convert';
import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:provider/provider.dart';
import 'package:study_online/entity/Category.dart';
import 'package:study_online/global/colors.dart';
import 'package:study_online/pages/studyPage/StudyModel.dart';

import '../../../main.dart';
import '../CourseStudyViewModel.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        //appBar: AppBar(
        //  backgroundColor: Colors.black26,
        //  title: Text('container'),
        //),
        body: VideoCatalogueTab(),
      ),
    );
  }
}

class VideoCatalogueTab extends StatefulWidget {
  @override
  _VideoCatalogueTabState createState() => _VideoCatalogueTabState();
}

class _VideoCatalogueTabState extends State<VideoCatalogueTab>
    with AutomaticKeepAliveClientMixin {
  BuildContext _context;
  var list = {
    '第一章,集合与函数': ['1.1集合', '1.2函数', '1.3集合与函数'],
    '第二章,导数': ['导', '数', '导', '数', '导', '数'],
    '第三章,圆锥曲线': ['圆', '锥', '曲', '线'],
    '第四章,空间集合': ['空', '间', '几', '何'],
    '第五章,向量': ['向', '量'],
    '第七章,解三角形': ['解', '三', '角', '形'],
  };
  CategoryList _categoryList = new CategoryList([]);
  List<int> _select = [];
  List<List<int>> _selectList = [];
  String videoAdr;

  _loadData() async {
    Response response = await getCategoryByCouId(1);
    print(response.toString());
    _categoryList = CategoryList.formJson(json.decode(response.toString()));
    //List responseData=json.decode(response.toString());
    setState(() {
      //_list=response;
      _categoryList = CategoryList.formJson(json.decode(response.toString()));
    });
  }

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    _context=context;
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    if (_categoryList.data.length==0) {
      return Container(
        width: 0,
        height: 0,
      );
    } else {
      for (int i = 0; i < _categoryList.data.length; ++i) {
        List<int> list = [];
        for (int j = 0; j < _categoryList.data[i].subTitle.length; ++j)
          list.add(0);
        _selectList.add(list);
      }
      _select.add(0);
      _select.add(0);
      _selectList[_select[0]][_select[1]] = 1;
      return Container(
        child: ListView(
          children: _buildList(context),
        ),
      );
    }
  }

  List<Widget> _buildList(BuildContext context) {
    List<Widget> widgets = [];
    for (int i = 0; i < _categoryList.data.length; ++i) {
      widgets.add(_item(_categoryList.data[i].titile.substring(1),
          _categoryList.data[i].subTitle, i));
    }
    return widgets;
  }

  Widget _item(String city, List<String> subCities, int index) {
    return Container(
      margin: EdgeInsets.only(
          top: ScreenUtil().setHeight(40),
          left: ScreenUtil().setWidth(35),
          right: ScreenUtil().setWidth(35)),
      padding: EdgeInsets.only(
          left: ScreenUtil().setWidth(35), right: ScreenUtil().setWidth(35)),
      decoration: BoxDecoration(
        color: Color(0xffF6F7F7),
        borderRadius: BorderRadius.all(Radius.circular(10)),
        boxShadow: [
          BoxShadow(
            color: Colors.black26,
            offset: Offset(-2, 2.0),
            blurRadius: 1.0,
            spreadRadius: 0.6,
          ),
          BoxShadow(
            color: Color(0xffF6F7F7),
          )
        ],
      ),
      child: ExpansionTile(
        title: Text(
          city,
          style: TextStyle(
            color: Colors.black,
            fontSize: ScreenUtil().setSp(55),
            fontWeight: FontWeight.bold,
          ),
        ),
        children:
            subCities.asMap().keys.map((ind) => _buildSub(index, ind)).toList(),
      ),
    );
  }

  Widget _buildSub(int index, int ind) {
    return Container(
      height: ScreenUtil().setHeight(70),
      margin: EdgeInsets.only(
          bottom: ScreenUtil().setHeight(20),
          left: ScreenUtil().setWidth(40),
          right: ScreenUtil().setWidth(40)),
      child: InkWell(
        onTap: () {
          _context.read<PublishRemarkViewModel>().setVideoAdr(_categoryList.data[index].vidAddr[ind]);
          videoAdr = _categoryList.data[index].vidAddr[ind];
          setState(() {
            _selectList[_select[0]][_select[1]] = 0;
            _select[0] = index;
            _select[1] = ind;
            _selectList[_select[0]][_select[1]] = 1;
          });
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                SizedBox(width: ScreenUtil().setWidth(35)),
                Icon(
                  Icons.slow_motion_video_sharp,
                  size: 16,
                ),
                SizedBox(
                  width: ScreenUtil().setWidth(20),
                ),
                Text(
                  _categoryList.data[index].subTitle[ind],
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(45),
                    color: _selectList[index][ind] == 0
                        ? Colors.black
                        : AppColors.primary,
                  ),
                ),
                Expanded(
                  child: Align(
                    alignment: Alignment.bottomRight,
                    child: Text(
                      '${Random().nextInt(7) +5}:${Random().nextInt(50)}',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(40),
                        color: AppColors.subtext,
                      ),
                    ),
                  ),
                )
              ],
            ),
          ],
        ),
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
